<template>
<!--  <d2-container class="mod-sys__user">-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="系统合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入系统合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择退款状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="contractRefundList" border stripe size="mini" @selection-change="handleSelectionChange">
      <el-table-column type="index" align="center" />
      <el-table-column label="系统合同编号" align="center" prop="contractNo" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="退款金额" align="center" prop="amount" />
      <el-table-column label="微信支付退款订单号" align="center" prop="wechatNo" />
      <el-table-column label="退款状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="退款原因" align="center" prop="applyDesc" />
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contract:contractRefund:approve']"
            v-if="scope.row.status !== '4'"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改合同退款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="系统合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" readonly/>
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" readonly/>
        </el-form-item>
        <el-form-item label="退款金额" prop="amount">
          <el-input v-model="form.amount" readonly/>
        </el-form-item>
        <el-form-item label="退款原因" prop="applyDesc">
          <el-input v-model="form.applyDesc" type="textarea" readonly/>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('2')">通过</el-button>
        <el-button type="danger" @click="submitForm('3')">不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
<!--  </d2-container>-->
</template>

<script>
import { listContractRefund, getContractRefund, delContractRefund, addContractRefund, exportContractRefund, approve } from "@/api/zfw/contract/contractRefund";

export default {
  name: "ContractRefund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同退款表格数据
      contractRefundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 退款状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        contractId: null,
        contractNo: null,
        contractName: null,
        wechatNo: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractNo: [
          { required: true, message: "系统合同编号不能为空", trigger: "blur" }
        ],
        contractName: [
          { required: true, message: "合同名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_contract_refund_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询合同退款列表 */
    getList() {
      this.loading = true;
      listContractRefund(this.queryParams).then(response => {
        this.contractRefundList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 退款状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractId: null,
        contractNo: null,
        contractName: null,
        amount: null,
        wechatNo: null,
        status: null,
        applyDesc: null,
        comment: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        valid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同退款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContractRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退款审核";
      });
    },
    /** 提交按钮 */
    submitForm (status) {
      this.form.status = status
      approve(this.form).then(response => {
        if (response.code === 100) {
          this.msgSuccess('操作成功')
          this.open = false
          this.getList()
        } else {
          this.msgError('操作失败')
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合同退款编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContractRefund(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同退款数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportContractRefund(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
